package com.un.ebs.businessObject.controller;

import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.domain.Field;
import com.un.ebs.businessObject.service.DataFieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 数据库操作表controller
 * @Author lcl
 * @Date 2021/01/20
 */
@Api(value = "dataField", description = "数据库操作")
@RestController
@RequestMapping("/dataField")
public class DataFieldController {

    @Autowired
    private DataFieldService dataFieldService;


    @ApiOperation("接口定义 : 创建表")
    @PostMapping(value = "createTable")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", dataType = "String", paramType = "query", value = "表名"),
            @ApiImplicitParam(name = "name", dataType = "String", paramType = "query", value = "DataSource表的name"),
    })
    public WebResult createTable(@RequestBody List<Field> list, String tableName, String name) {
        return WebUtils.success(dataFieldService.createTable(list,tableName,name));
    }

    @ApiOperation("接口定义 : 删除表")
    @PostMapping(value = "deleteTable")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", dataType = "String", paramType = "query", value = "表名"),
            @ApiImplicitParam(name = "name", dataType = "String", paramType = "query", value = "DataSource表的name"),
    })
    public WebResult deleteTable(String tableName,String name) {
        return WebUtils.success(dataFieldService.dropTable(tableName, name));
    }

    @ApiOperation("接口定义 : 增加表字段")
    @PostMapping(value = "addField")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", dataType = "String", paramType = "query", value = "表名"),
            @ApiImplicitParam(name = "name", dataType = "String", paramType = "query", value = "DataSource表的name"),
    })
    public WebResult addField(@RequestBody Field field,String tableName,String name) {
        return WebUtils.success(dataFieldService.addField(field,tableName,name));
    }

    @ApiOperation("接口定义 : 删除表字段")
    @PostMapping(value = "delField")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", dataType = "String", paramType = "query", value = "表名"),
            @ApiImplicitParam(name = "name", dataType = "String", paramType = "query", value = "DataSource表的name"),
    })
    public WebResult delField(@RequestBody Field field,String tableName,String name) {
        return WebUtils.success(dataFieldService.delField(field,tableName,name));
    }

    @ApiOperation("接口定义 : 修改表字段")
    @PostMapping(value = "updateField")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", dataType = "String", paramType = "query", value = "表名"),
            @ApiImplicitParam(name = "oldFieldName", dataType = "String", paramType = "query", value = "旧字段名"),
            @ApiImplicitParam(name = "name", dataType = "String", paramType = "query", value = "DataSource表的name"),
    })
    public WebResult updateField(@RequestBody Field field,String tableName,String oldFieldName,String name) {
        return WebUtils.success(dataFieldService.updateField(field,tableName,oldFieldName,name));
    }

    @ApiOperation("接口定义 : 表是否创建")
    @PostMapping(value = "isExistTable")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", dataType = "String", paramType = "query", value = "表名"),
            @ApiImplicitParam(name = "name", dataType = "String", paramType = "query", value = "DataSource表的name"),
    })
    public WebResult isExistTable(String tableName,String name) {
        return WebUtils.success(dataFieldService.isExistTable(tableName,name));
    }
}
